package com.pactera.asmp.server.restfullapi;

import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.pojo.TPluginConfig;
import com.pactera.asmp.server.service.IPluginConfigService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.restfullapi
 * @ClassName: RestfulPluginConfigApi
 * @Description: RestfulPluginConfigApi
 * @Date 2020/05/30 22:59
 * @Author lvzhiqiang
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/pluginConfig")
@Slf4j
public class RestfulPluginConfigApi {
    @Autowired
    private IPluginConfigService pluginConfigService;

    /**
     * @api {POST} /api/v1/pluginConfig/add add
     * @apiVersion 1.0.0
     * @apiGroup RestfulPluginConfigApi
     * @apiName add
     * @apiDescription 添加插件配置接口
     * @apiParam (请求体) {Number} projectId 项目ID
     * @apiParam (请求体) {String} serverAddress 服务地址
     * @apiParam (请求体) {String} configItem 配置项
     * @apiParam (请求体) {String} exceptionDataCollection 异常数据收集
     * @apiParamExample 请求体示例
     * {"exceptionDataCollection":"m9v1OCJ","serverAddress":"C44","projectId":9948,"configItem":"QapCxk4"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":5101,"data":{},"message":"ncm87N"}
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody TPluginConfig pluginConfig) {
        log.debug("/api/v1/pluginConfig/add pluginConfig: {}", pluginConfig);
        if (null == pluginConfig) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(pluginConfigService.insert(pluginConfig));
        return responseResult;
    }


    /**
     * @api {GET} /api/v1/pluginConfig/list list
     * @apiVersion 1.0.0
     * @apiGroup RestfulPluginConfigApi
     * @apiName list
     * @apiDescription 获取插件配置列表接口
     * @apiParam (请求参数) {Number} page 页号
     * @apiParam (请求参数) {Number} limit 页大小
     * @apiParamExample 请求参数示例
     * limit=4059&page=1602
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":6758,"data":{},"message":"I"}
     */
    @GetMapping("/list")
    public ResponseResult list(@RequestParam(value ="page", required = false)Integer page,
            @RequestParam(value ="limit", required = false)Integer limit,
            @RequestParam(value ="projectId", required = false)Integer projectId) {
        log.debug("/api/v1/pluginConfig/list page: {}, limit: {}, projectId:{}", page, limit, projectId);
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(pluginConfigService.selective(page, limit, projectId));
        return responseResult;
    }

    /**
     * @api {DELETE} /api/v1/pluginConfig/delete delete
     * @apiVersion 1.0.0
     * @apiGroup RestfulPluginConfigApi
     * @apiName delete
     * @apiDescription 根据主键删除插件配置接口
     * @apiParam (请求参数) {Number} id 插件配置表主键
     * @apiParamExample 请求参数示例
     * id=8345
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":4590,"data":{},"message":"hw1ddCsC73"}
     */
    @DeleteMapping("/delete")
    public ResponseResult delete(Long id) {
        log.debug("/api/v1/pluginConfig/delete id: {}", id);
        if (null == id || 0 == id) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        TPluginConfig pluginConfig = new TPluginConfig();
        pluginConfig.setId(id);
        pluginConfig.setIsUse((byte) 1);
        responseResult.setData(pluginConfigService.update(pluginConfig));
        return responseResult;
    }

    /**
     * @api {GET} /api/v1/pluginConfig/detail detail
     * @apiVersion 1.0.0
     * @apiGroup RestfulPluginConfigApi
     * @apiName detail
     * @apiDescription 根据主键获取插件配置详情
     * @apiParam (请求参数) {Number} id 插件配置表主键
     * @apiParamExample 请求参数示例
     * id=6821
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":6314,"data":{},"message":"DyJszA"}
     */
    @GetMapping("/detail")
    public ResponseResult detail(Long id) {
        log.debug("/api/v1/pluginConfig/detail id: {}", id);
        if (null == id || id <= 0) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(pluginConfigService.detail(id));
        return responseResult;
    }

    /**
     * @api {PUT} /api/v1/pluginConfig/reCreateApk reCreateApk
     * @apiVersion 1.0.0
     * @apiGroup RestfulPluginConfigApi
     * @apiName reCreateApk
     * @apiDescription 重新生成apk接口
     * @apiParam (请求参数) {Number} id 插件配置表主键
     * @apiParamExample 请求参数示例
     * id=3925
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":5671,"data":{},"message":"4JJqBC"}
     */
    @PutMapping("/reCreateApk")
    public ResponseResult reCreateApk(Long id) {
        log.debug("/api/v1/pluginConfig/reCreateApk id: {}", id);
        if (null == id || id <= 0) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        TPluginConfig pluginConfig = pluginConfigService.detail(id);
        if (null == pluginConfig) {
            return new ResponseResult(ResultCode.RESULT_DATA_NONE);
        }
        pluginConfigService.sendMsgAndChangeStatus(pluginConfig);
        return responseResult;
    }
}
